package com.cw.jeeyt.controller.elasticsearch;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.bo.elasticsearch.ElasticDocDto;
import com.cw.jeeyt.service.elasticsearch.IElasticService;
import com.cw.lang.common.utils.GsonUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/elastic")
public class ElasticController {
    @Autowired
    private IElasticService elasticService;

    @GetMapping("/init")
    @ResponseBody
    public void init(){
        elasticService.createIndex();
        List<ElasticDocDto> list =new ArrayList<>();
        list.add(new ElasticDocDto(1L,"XX0193","XX8064","xxxxxx",1));
        list.add(new ElasticDocDto(2L,"XX0210","XX7475","xxxxxxxxxx",1));
        list.add(new ElasticDocDto(3L,"XX0257","XX8097","xxxxxxxxxxxxxxxxxx",1));
        elasticService.saveAll(list);
    }

    @GetMapping("/all")
    @ResponseBody
    public JsonResultDto all(){
        Page<ElasticDocDto> page = elasticService.findByFirstCode("0193");
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        Iterator<ElasticDocDto> iterator = elasticService.findAll();
        jsonResultDto.setData(GsonUtil.objectToJsonString(page.getContent()));
        return jsonResultDto;
    }
}
